//
//  XPSearchBar.swift
//  PPKit
//
//  Created by xiaopin on 2020/7/23.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//

import UIKit

class XPSearchBar: UISearchBar,UITextFieldDelegate {
    
    /// icon宽度
    var searchIconW:CGFloat = 20.0
    /// icon与placeholder间距
    var iconSpacing:CGFloat = 10.0
    /// 占位文字的字体大小
    var searchFont:UIFont = .systemFont(ofSize: 14, weight: .light)
    /// placeholder 和icon 和 间隙的整体宽度
    var placeholderWidth: CGFloat {
        let autoWidth = self.placeholder?.autoWidth(font: searchFont, height: 20) ?? 0
        return autoWidth + iconSpacing + searchIconW
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //let backImage = UIImage.colorImage(.red)
        //self.setBackgroundImage(backImage, for: .any, barMetrics: .default)
        
        self.barStyle = .default
        self.searchBarStyle = .minimal
        
        if #available(iOS 13, *){
            self.searchTextField.cornerRadius = 20
            self.searchTextField.backgroundColor = pp_color(#colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1), #colorLiteral(red: 0.1490196078, green: 0.1490196078, blue: 0.168627451, alpha: 1))
            self.searchTextField.font = searchFont
            self.searchTextField.attributedPlaceholder = NSMutableAttributedString(string: self.placeholder ?? "搜索", attributes: [.foregroundColor:pp_color(.CCCCCCC),.font:searchFont])
            self.searchTextField.delegate = self
            self.setPositionAdjustment(UIOffset(horizontal: (self.searchTextField.frame.size.width-self.placeholderWidth)/2, vertical: 0), for: .search)
        }else{
            if let subviews = self.subviews.last?.subviews {
                for view in subviews {
                    if view.isKind(of: UITextField.classForCoder()){
                        let field = view as! UITextField
                        field.frame = CGRect(x: 15.0, y: 7.5, width: self.frame.size.width - 30.0, height: self.frame.size.height - 15.0)
                        field.backgroundColor = pp_color(.CF8F8F8)
                        field.textColor = pp_color(.C222222)
                        field.font = searchFont
                        field.borderStyle = .none
                        field.cornerRadius = (self.frame.size.height - 15.0) / 2.0
                        field.attributedPlaceholder = NSAttributedString(string: self.placeholder ?? "搜索", attributes: [.foregroundColor:#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0),.font:searchFont])
                        self.setPositionAdjustment(UIOffset(horizontal: (field.frame.size.width-self.placeholderWidth)/2, vertical: 0), for: .search)
                    }
                }
            }
        }
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        _ = self.delegate?.searchBarShouldBeginEditing?(self)
        self.setPositionAdjustment(.zero, for: .search)
        return true
    }
    
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        _ = self.delegate?.searchBarShouldEndEditing?(self)
        if textField.text?.isEmpty == false {
            self.setPositionAdjustment(.zero, for: .search)
        }else{
            self.setPositionAdjustment(UIOffset(horizontal: (textField.frame.size.width-self.placeholderWidth)/2, vertical: 0), for: .search)
        }
        return true
    }
}
